package freelinz.smart.gwtex.client.ui;


import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.SimplePanel;

import freelinz.smart.gwtex.client.ui.SmartKitsAll.Resized;
import freelinz.smart.gwtex.user.client.DOMEx;

/**
 * 多文档视的根panel，会自适应浏览器大小，禁用浏览器的滚动条
 * 
 * @author wpeace
 * 
 */
public class FirstPanel extends SimplePanel
{
    private Resized resized;

    private final int margin = 0;

    public FirstPanel(Resized resized)
    {
        super();
        this.resized = resized;
        // 禁用浏览器的滚动条
//        Window.enableScrolling(false);
        Window.setMargin(margin + "px");
        if(this.resized != null)
        {
            add(this.resized.getWidget());
        }
    }

    protected void onLoad()
    {
        super.onLoad();
        Logger.debug("FirstPanel.getClientWidth():"
                + FirstPanel.getClientWidth());
        Logger.debug("FirstPanel.getClientHeight():"
                + FirstPanel.getClientHeight());
        resized(DOMEx.getClientWidthNoScrollBar() - margin * 2, 
                DOMEx.getClientHeightNoScrollBar() - margin * 2);
        Window.addWindowResizeListener(new WindowResizeListener()
        {
            public void onWindowResized(int width, int height)
            {
                Logger.debug("\r\nstart");
                resized(DOMEx.getClientWidthNoScrollBar() - margin * 2, 
                        DOMEx.getClientHeightNoScrollBar() - margin * 2);
            }
        });

    }

    private void resized(int width, int height)
    {
        Logger.debug(FirstPanel.class.toString() + " width:" + width);
        Logger.debug(FirstPanel.class.toString() + " heigth" + height);
        this.setSize(width + "px", height + "px");
        if(this.resized != null)
        {
            this.resized.resized(width, height);
        }
    }

    /**
     * 客户区的高度不包含浏览器的滚动条
     * 
     * @return 返回客户区的有效高度
     */
    public static native int getClientHeight() /*-{
         return $doc.body.clientHeight;
    }-*/;

    /**
     * 客户区的高度不包含浏览器的滚动条
     * 
     * @return 返回客户区的有效宽度
     */
    public static native int getClientWidth()/*-{
         return $doc.body.clientWidth;
    }-*/;

    // public static native int getClientHeight() /*-{
    // if ($wnd.innerHeight)
    // return $wnd.innerHeight;
    // return $doc.body.clientHeight;
    // }-*/;
    //
    // public static native int getClientWidth() /*-{
    //      if ($wnd.innerWidth)
    //        return $wnd.innerWidth;
    //      return $doc.body.clientWidth;
    //    }-*/;
}
